﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Samples_FirstGrid : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.dotNetFlexGrid1.InitConfig(
            new string[]{
                "title=演示表格",//标题
                "striped=true",//是否显示行交替色
                "selectedonclick=true",//是否点击行自动选中checkbox
                "usepager=true",//使用分页器
                "showcheckbox=true",//显示复选框
                "height=250",//高度，可为auto或具体px值
                "width=600"//宽度，可为auto或具体px值
            },
            new dotNetFlexGrid.FieldConfig[]{
                new dotNetFlexGrid.FieldConfig("Guid","编号",60,true,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("String1","用户名",120,true,dotNetFlexGrid.FieldConfigAlign.Right),
                new dotNetFlexGrid.FieldConfig("String2","地址",120,false,dotNetFlexGrid.FieldConfigAlign.Left,false,false,true,"<a href='http://www.google.com.hk/search?q=[@String2]'>访问[@String1]的地址</a>"),
                new dotNetFlexGrid.FieldConfig("String3","创建时间",60,true,dotNetFlexGrid.FieldConfigAlign.Left),
                new dotNetFlexGrid.FieldConfig("Image","照片",160,false,dotNetFlexGrid.FieldConfigAlign.Left,false,false,true,"<img src='http://www.cnblogs.com/images/logo_small.gif'/>")
            },
            new dotNetFlexGrid.SerchParamConfig[]{
                new dotNetFlexGrid.SerchParamConfig("用户名","String1",true, dotNetFlexGrid.SerchParamConfigOperater.Eq,null),//通过String1进行相等查询，不绑定正则表达式
                new dotNetFlexGrid.SerchParamConfig("地址","String2",false, dotNetFlexGrid.SerchParamConfigOperater.Like,@"^[\d]+$")//通过String2进行近似查询，绑定必须输入数字
            },
            null
        );
        this.dotNetFlexGrid1.DataHandler = new dotNetFlexGrid.DataHandlerDelegate(DotNetFlexGrid1DataHandler);  //提供数据的方法

        
    }
    public dotNetFlexGrid.DataHandlerResult DotNetFlexGrid1DataHandler(dotNetFlexGrid.DataHandlerParams p)
    {
        dotNetFlexGrid.DataHandlerResult result = new dotNetFlexGrid.DataHandlerResult();
        result.page = p.page;//设定当前返回的页号
        result.total = 100;//总计的数据条数，此处用100进行模拟，查询和筛选时需要根据实际
        result.table = dotNetFlexGrid.DemoMemoryTable(p.page * 100, p.rp);//调用演示的数据生成函数产生模拟数据
        //如果传递的参数包含排序设置的话，通过DataView.Sort功能模拟进行当页排序
        if (p.sortname.Length > 0 && p.sortorder.Length > 0)
        {
            System.Data.DataView dv = result.table.DefaultView;
            dv.Sort = (p.sortname + " " + p.sortorder);
            result.table = dv.ToTable();
        }
        //处理默认查询，即Button1_Click中指定的DefaultSearch查询参数
        if (p.defaultSearch.ContainsKey("String1"))
        {
            System.Data.DataView dv = result.table.DefaultView;
            dv.RowFilter = "String1 Like '%" + p.defaultSearch["String1"] + "%'";
            result.table = dv.ToTable();
        }

        //如果传递的参数包含附加参数的话，再来模拟查询，原谅我真的很懒。
        if (p.extParam.ContainsKey("String1"))
        {
            System.Data.DataView dv = result.table.DefaultView;
            dv.RowFilter = "String1 Like '%" + p.extParam["String1"] + "%'";
            result.table = dv.ToTable();
        }
        //如果传递的参数包含快速查询参数，则进行快速查询
        if (p.qop != dotNetFlexGrid.SerchParamConfigOperater.None && p.qtype.Length > 0 && p.query.Length > 0)
        {
            System.Data.DataView dv = result.table.DefaultView;
            if(p.qop== dotNetFlexGrid.SerchParamConfigOperater.Like)
                dv.RowFilter = p.qtype+ " Like '%" + p.query + "%'";
            else
                dv.RowFilter = p.qtype + " = '" + p.query + "'";
            result.table = dv.ToTable();
        }

        return result;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        dotNetFlexGrid1.DefaultSearch.Add("String1", TextBox1.Text);//使用String1这个字段进行查询
    }
}
